package com.ewanghuiju.app.presenter.my;

import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.my.AddAddressContract;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.request.AddressRequestBean;
import com.ewanghuiju.app.model.bean.response.AddressListResponBean;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import javax.inject.Inject;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/16
 */
public class AddAddressPresenter extends RxPresenter<AddAddressContract.View> implements AddAddressContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public AddAddressPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void attachView(AddAddressContract.View view) {
        super.attachView(view);
//        registerEvent();
    }

    @Override
    public void addData(AddressRequestBean addressRequestBean) {
        addSubscribe(mDataManager.addPtiaddress(addressRequestBean)
                        .compose(RxUtil.rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<AddressListResponBean>>(mView) {
                            @Override
                            public void onNext(Optional<AddressListResponBean> optional) {
                                if (mView != null)
                                    mView.showContent(optional.getIncludeNull());
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                        if (e instanceof ApiException) {
//                        mView.showLoginResponseMsg(((ApiException) e).getCode(), e.getMessage());
//                        } else {
//                        }
                            }
                        })
        );

    }

    @Override
    public void editData(AddressRequestBean addressRequestBean) {
        addSubscribe(mDataManager.addPtiaddressEdit(addressRequestBean)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<AddressListResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<AddressListResponBean> optional) {
                        if (mView != null)
                            mView.showContent(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }
}
